package impl

import (
	"context"
	"fmt"
	"time"

	"dario.cat/mergo"
	"gitee.com/liuxiaowei123/vblog-1.1/app/blog"
)

func (b *BlogServiceImpl) CreateBlog(c context.Context, req *blog.CreateBlogRequest) (*blog.Blog, error) {
	ins := blog.NewBlog(req)
	err := b.db.WithContext(c).Create(ins).Error
	if err != nil {
		return nil, err
	}
	return ins, nil
}

// 查询的是博客列表
func (b *BlogServiceImpl) QueryBlog(c context.Context, req *blog.QueryBlogRequest) (*blog.BlogSet, error) {
	// 查询条件
	query := b.db.WithContext(c).Model(&blog.Blog{})
	ins := blog.NewBlogSet()
	// 根据状态查询
	if req.Status != nil {
		query = query.Where("status = ?", req.Status)
	}
	// 根据标题关键词查询
	if req.Keywords != "" {
		query = query.Where("title LIKE ?", "%"+req.Keywords+"%")
	}
	// 根据blog用户查询
	if len(req.Usernames) > 0 {
		query = query.Where("create_by in ?", req.Usernames)
	}

	// 1.查询博客数量
	err := query.Count(&ins.Total).Error
	if err != nil {
		return nil, err
	}

	// 2.查询当前页的博客列表
	err = query.Order("created_at DESC").Offset(req.Offset()).Limit(req.PageSize).Find(&ins.Item).Error
	if err != nil {
		return nil, err
	}

	return ins, nil
}

// 通过id来查询blog
func (b *BlogServiceImpl) DescribeBlog(c context.Context, req *blog.DescribeBlogRequest) (*blog.Blog, error) {
	query := b.db.WithContext(c).Model(&blog.Blog{})
	ins := blog.NewBlog(blog.NewCreateBlogRequest())
	err := query.Where("id = ?", req.BlogId).First(ins).Error
	if err != nil {
		return nil, err
	}
	return ins, nil
}

func (b *BlogServiceImpl) UpdateBlogStatus(c context.Context, req *blog.UpdateBlogStatusRequest) (
	*blog.Blog, error) {
	return nil, nil
}

func (b *BlogServiceImpl) UpdateBlog(c context.Context, req *blog.UpdateBlogRequest) (*blog.Blog, error) {
	// 1.查询用户
	fmt.Println(req.BlogId)
	ins, err := b.DescribeBlog(c, blog.NewDescribeBlogRequest(req.BlogId))
	if err != nil {
		return nil, err
	}
	fmt.Println(ins)
	// 2.判断更新类型
	switch req.UpdateMode {
	case blog.UPDATE_MODE_PUT:
		ins.CreateBlogRequest = req.CreateBlogRequest
	case blog.UPDATE_MODE_PATCH:
		err = mergo.Merge(ins.CreateBlogRequest, req.CreateBlogRequest, mergo.WithOverride)
		if err != nil {
			return nil, err
		}
	default:
		fmt.Printf("不知道%v的更新类型", req.UpdateMode)
	}

	// 3.更新数据
	ins.UpdatedAt = time.Now().Unix()
	err = b.db.WithContext(c).Updates(ins).Error
	if err != nil {
		return nil, err
	}

	return ins, nil
}
func (b *BlogServiceImpl) DeleteBlog(c context.Context, req *blog.DeleteBlogRequest) error {
	// 查询
	// 错误代码 ins, err := b.DescribeBlog(c, blog.NewDescribeBlogRequest("req.BlogId"))
	ins, err := b.DescribeBlog(c, blog.NewDescribeBlogRequest(req.BlogId))
	if err != nil {
		return err
	}

	// 删除
	err = b.db.WithContext(c).Delete(ins).Error
	if err != nil {
		return err
	}
	return nil
}
func (b *BlogServiceImpl) AuditBlog(c context.Context, req *blog.AuditBlogRequest) (*blog.Blog, error) {
	// 查询
	ins, err := b.DescribeBlog(c, blog.NewDescribeBlogRequest("req.BlogId"))
	if err != nil {
		return nil, err
	}

	// 审核
	ins.AuditAt = time.Now().Unix()
	ins.IsAuditPass = req.IsAuditPass
	err = b.db.WithContext(c).Updates(ins).Error
	if err != nil {
		return nil, err
	}

	return ins, nil
}
